package cn.itcast.bos.web.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import cn.itcast.bos.domain.Region;
import cn.itcast.bos.utils.PinYin4jUtils;
import cn.itcast.bos.web.action.base.BaseAction;
import net.sf.json.JSONArray;
import net.sf.json.JsonConfig;

@Controller
@Scope("prototype")
public class RegionAction extends BaseAction<Region>{
	//接收上传的文件
	private File myFile;
	public void setMyFile(File myFile) {
		this.myFile = myFile;
	}
	
	//获得上传文件的文件名
	private String myFileFileName;
	public void setMyFileFileName(String myFileFileName) {
		this.myFileFileName = myFileFileName;
	}
	
	public String uploadXLS() throws FileNotFoundException, IOException{
		
		String flag="1";
		List<Region> list = new ArrayList<>();
		try {
			Workbook workbook=null;
			if (myFileFileName.endsWith(".xlsx")) {
				workbook = new XSSFWorkbook(new FileInputStream(myFile));
			}else {
				workbook = new HSSFWorkbook(new FileInputStream(myFile));
			}
			//获得Exel文档的第一个sheet
			Sheet sheet = workbook.getSheetAt(0);
			for (Row row : sheet) { //获得 sheet 中列的集合
				int rowNum = row.getRowNum();  //获得行号
				if (rowNum ==0) {
					continue;   //过滤标题行
				}
				
				String id = row.getCell(0).getStringCellValue();  //获得第一列的值
				String province = row.getCell(1).getStringCellValue(); 
				String city = row.getCell(2).getStringCellValue(); 
				String district = row.getCell(3).getStringCellValue(); 
				String postcode = row.getCell(4).getStringCellValue(); 
				
				Region region = new Region(id, province, city, district, postcode);
				
				String[] headStrings = PinYin4jUtils.getHeadByString(province+city+district);
				String shortcode = StringUtils.join(headStrings, "");
				
				String[] pinyin = PinYin4jUtils.stringToPinyin(city);
				String citycode = StringUtils.join(pinyin);
				region.setShortcode(shortcode);
				region.setCitycode(citycode);
				list.add(region);
			}
			
			regionService.uploadXLS(list);
		} catch (Exception e) {
			flag="2";
			e.printStackTrace();
		}
		
		ServletActionContext.getResponse().getWriter().print(flag);
		return NONE;
	}
	
	//pageRegion  分页显示
	public String pageRegion(){
		regionService.pageQueryList(pageBean);
		pageList(pageBean, new String[]{"detachedCriteria","pageSize","currentPage","subareas"});
		return NONE;
	}
	
	
	//管理分区中的 comBox选择分区selectRegion
	public String selectRegion(){
		List<Region> list =regionService.findAll();
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig .setExcludes(new String[]{"province","city","district","citycode","shortcode","postcode","subareas"});
		JSONArray jsonArray = JSONArray.fromObject(list, jsonConfig);
		String json = jsonArray.toString();
		ServletActionContext.getResponse().setContentType("text/json;charset=utf-8");
		try {
			ServletActionContext.getResponse().getWriter().print(json);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return NONE;
	}
	
}
